#!/usr/bin/python
# -*- coding: utf-8 -*-

import maya.cmds as mc
import LZ_realistBodySys_commands
reload(LZ_realistBodySys_commands)
import LZ_realistBodySys_init
reload(LZ_realistBodySys_init)
'''
-------------------------------------------------------------------------------------

'''
"""
        #=====================================================#
        #         Script Name: mirror locator temple   #
        #         Author:   lizhi                             #
        #         start date: Oct 31, 2014                    #
        #         start Updated: Oct 31, 2014                 #
        #         Update/Change this file at:                 #
        #         company: http://www.originalforce.com/      #
        #         Email:294596787@qq.com                      #
        # Please do not alter any information above this line #
        #=====================================================#
        #
        #python                                               #
        #directions (for use):                                #
        #                                                     #
        #                                                     #
        #=====================================================#
        """  
class LZ_realistBodySys_SeketonBuilder(LZ_realistBodySys_commands.LZ_realistBodySys_commands , LZ_realistBodySys_init.LZ_realistBodySys_init):
    def __init__(self):
    
        LZ_realistBodySys_init.LZ_realistBodySys_init.__init__(self)
        #  spine 
        self.hip_JntTemple = self.centerPoint_hip[0]
        self.hipEnd_JntTemple = self.centerPoint_hipEnd[0]
        self.spine1_JntTemple = self.centerPoint_spine1[0]
        self.spine2_JntTemple = self.centerPoint_spine2[0]
        self.chest_JntTemple = self.centerPoint_chest[0]
        self.neck_JntTemple = self.centerPoint_neck[0]
        self.head_JntTemple = self.centerPoint_head[0]
        self.headEnd_JntTemple = self.centerPoint_headEnd[0]
        self.jaw_JntTemple = self.centerPoint_jaw[0]
        self.jawEnd_JntTemple = self.centerPoint_jawEnd[0]
        ##```````````````````````````````````````````````````````````````````
        #   arm 
        self.shoulder_JntTemple = self.L_centerPoint_shoulder[0]
        self.clavicle_JntTemple = self.L_centerPoint_clavicle[0]
        self.arm_JntTemple = self.L_centerPoint_arm[0]
        self.foreArm_JntTemple = self.L_centerPoint_foreArm[0]
        self.hand_JntTemple = self.L_centerPoint_hand[0]
        
        ##```````````````````````````````````````````````````````````````````
        #   leg
        self.upleg_JntTemple = self.L_centerPoint_upleg[0]
        self.leg_JntTemple = self.L_centerPoint_leg[0]
        self.foot_JntTemple = self.L_centerPoint_foot[0]
        self.toe_JntTemple = self.L_centerPoint_toe[0]
        self.toeend_JntTemple = self.L_centerPoint_toeend[0]
        
        self.inside_JntTemple = self.L_centerPoint_inside[0]
        self.outside_JntTemple = self.L_centerPoint_outside[0]
        self.toeDnEnd_JntTemple = self.L_centerPoint_toeDnEnd[0]
        self.reverseHeel_JntTemple = self.L_centerPoint_reverseHeel[0]
        
        self.ankle_JntTemple = self.locator_foot[0]
        
    def LZ_realistBodySys_createBaseJnt(self):
        skeletonGrp = 'LZ_realistBodySys_Skeleton_grp'
        ####
        if mc.objExists(skeletonGrp) == 0:
            mc.group(em = True ,n = skeletonGrp )
            
        else:
            pass
        
        for temple  in [self.hip_JntTemple, self.hipEnd_JntTemple,self.spine1_JntTemple,self.spine2_JntTemple,self.chest_JntTemple,self.neck_JntTemple,self.head_JntTemple,self.headEnd_JntTemple,self.jaw_JntTemple,self.jawEnd_JntTemple 
                        ,self.shoulder_JntTemple,self.clavicle_JntTemple,self.arm_JntTemple,self.foreArm_JntTemple,self.hand_JntTemple 
                        ,self.upleg_JntTemple,self.leg_JntTemple,self.foot_JntTemple,self.toe_JntTemple,self.toeend_JntTemple ,self.ankle_JntTemple
                        ,self.inside_JntTemple,self.outside_JntTemple,self.toeDnEnd_JntTemple,self.reverseHeel_JntTemple ]:
            
            skeleton = str(temple).replace('_Temple','_skeleton')
            self.LZ_creatJoint(temple,skeleton)
            
            mc.parent( skeleton , skeletonGrp)
            
            if 'hip' in temple:
                skeletonDn = str(temple).replace('_Temple','_Dn_skeleton')
                self.LZ_creatJoint(temple,skeletonDn)
                
                mc.parent( skeletonDn , skeletonGrp)
            # mirror R_jnt
            
            if 'L_' in temple :
                R_temple = str(temple).replace('L_','R_')
                R_skeleton = str(skeleton).replace('L_','R_')
                self.LZ_creatJoint(R_temple,R_skeleton)
                
                
        
                mc.parent( R_skeleton , skeletonGrp)
        
        #
        ''' 
        #------------------------------------------------------------
        |||              spine 
        '''
        spineSkeleton_all = []
        
        spineSkeleton = [ self.hip_JntTemple,self.spine1_JntTemple,self.spine2_JntTemple,self.chest_JntTemple,self.neck_JntTemple,self.head_JntTemple,self.headEnd_JntTemple  ]
        for spine_num in range(len(spineSkeleton)):
            if spine_num == len(spineSkeleton) - 1:
                skeleton = str(spineSkeleton[spine_num]).replace('_Temple','_skeleton')
                spineSkeleton_all.append( skeleton )
            else:
                skeleton = str(spineSkeleton[spine_num]).replace('_Temple','_skeleton')
                
                
                mc.aimConstraint( spineSkeleton[spine_num + 1] , skeleton ,aimVector = (1,0,0), upVector = (0,0,1),worldUpType = 'vector',worldUpVector= (1,0,0) , n = 'spineTemple_deleteConstraint')
                mc.delete('spineTemple_deleteConstraint')
                
                self.frozeen_jnt_Transform( skeleton )
                spineSkeleton_all.append( skeleton )
                
        self.sequenceParent(spineSkeleton_all[::-1])
        ''' 
        #------------------------------------------------------------
        |||              spine Dn
        '''
        spineDnSkeleton = str(self.hip_JntTemple).replace('_Temple','_Dn_skeleton')
        spineDnTipSkeleton = str(self.hipEnd_JntTemple).replace('_Temple','_skeleton')
        hipSkeleton = str(self.hip_JntTemple).replace('_Temple','_skeleton')
        
        mc.aimConstraint( spineDnTipSkeleton , spineDnSkeleton ,aimVector = (1,0,0), upVector = (0,0,1),worldUpType = 'vector',worldUpVector= (1,0,0) , n = 'spineTemple_deleteConstraint')
        mc.delete('spineTemple_deleteConstraint')
        self.frozeen_jnt_Transform( spineDnSkeleton )
        spineSkeleton_all.append( spineDnSkeleton )
        
        mc.parent( spineDnTipSkeleton, spineDnSkeleton)
        mc.parent(  spineDnSkeleton , hipSkeleton )
        ####
        #if mc.objExists(skeletonGrp) == 0:
        #    mc.group(em = True ,n = skeletonGrp )
        #    
        #else:
        #    pass
        #try:
        #    mc.parent( hipSkeleton , skeletonGrp)
        #except:
        #    pass
        #
        ''' 
        #------------------------------------------------------------
        |||             arm
        '''
        armSkeleton = [ self.arm_JntTemple,self.foreArm_JntTemple,self.hand_JntTemple ]
        armSkeleton_all = []
        R_armSkeleton_all = []
        
        R_jntTemple = []
        for armS_num  in range(len(armSkeleton)):
            

                R_temple = str(armSkeleton[armS_num]).replace('L_','R_')
                R_jntTemple.append( R_temple )
        ###
        L_armReference = 'L_arm_up_reference'
        mc.group(em = True ,n = L_armReference )
        PointC = 'arm_delete_PointC'
        aimC = 'arm_delete_aimC'
        mc.pointConstraint(armSkeleton[0],armSkeleton[-1], L_armReference ,n= PointC )
        mc.aimConstraint( armSkeleton[-1]  ,L_armReference  ,worldUpType='object',aimVector = (1,0,0), upVector = (0,0,-1),worldUpObject = armSkeleton[1] , n =aimC)
        
        mc.delete( PointC , aimC )
        ####
        R_armReference = 'R_arm_up_reference'
        mc.group(em = True ,n = R_armReference )
        PointC = 'arm_delete_PointC'
        aimC = 'arm_delete_aimC'

        mc.pointConstraint(R_jntTemple[0],R_jntTemple[-1], R_armReference ,n= PointC )
        mc.aimConstraint( R_jntTemple[-1]  ,R_armReference  ,worldUpType='object',aimVector = (-1,0,0), upVector = (0,0,1),worldUpObject = R_jntTemple[1] , n =aimC)
        
        mc.delete( PointC , aimC )
        
        
        
        for armS_num  in range(len(armSkeleton)):
            
            if armS_num == len(armSkeleton) - 1:
                skeleton = str(armSkeleton[armS_num]).replace('_Temple','_skeleton')
                
                
                
                armSkeleton_all.append( skeleton )
            else:
                skeleton = str(armSkeleton[armS_num]).replace('_Temple','_skeleton')
                
                
                mc.aimConstraint( armSkeleton[armS_num + 1] , skeleton ,aimVector = (1,0,0), upVector = (0,1,0),worldUpType = 'objectrotation',worldUpObject= L_armReference ,worldUpVector= (0,1,0) , n = 'armTemple_deleteConstraint')
                mc.delete('armTemple_deleteConstraint')
                
                self.frozeen_jnt_Transform( skeleton )
                armSkeleton_all.append( skeleton )
                
            
            '''
            #``````````````````````````````` mirror arm ````````````````````````````````````````````
            '''
            

            R_skeleton = str( skeleton ).replace('L_','R_')

            R_armSkeleton_all.append( R_skeleton )
            

            if armS_num == len(armSkeleton) - 1:
                pass

            else:
                R_skeleton_Dn = str(armSkeleton[armS_num + 1]).replace('L_','R_')
                #mc.aimConstraint( R_skeleton_Dn , R_skeleton ,aimVector = (-1,0,0), upVector = (0,1,0),worldUpType = 'vector',worldUpVector= (0,-1,0) ,w=1, n = 'R_armTemple_deleteConstraint')
                mc.aimConstraint( R_skeleton_Dn , R_skeleton ,aimVector = (-1,0,0), upVector = (0,1,0),worldUpType = 'objectrotation',worldUpObject= R_armReference ,worldUpVector= (0,1,0) , n = 'R_armTemple_deleteConstraint')
                mc.delete('R_armTemple_deleteConstraint')
                
                self.frozeen_jnt_Transform( R_skeleton )


        self.sequenceParent(armSkeleton_all[::-1])
        self.sequenceParent(R_armSkeleton_all[::-1])
        
        mc.delete(L_armReference,R_armReference)
        
        #try:
        #    mc.parent( armSkeleton_all[0] , skeletonGrp)
        #    mc.parent( R_armSkeleton_all[0] , skeletonGrp)
        #except:
        #    pass
        
        ''' 
        #------------------------------------------------------------
        |||             leg
        '''
        
        legSkeleton = [ self.upleg_JntTemple,self.leg_JntTemple,self.foot_JntTemple,self.toe_JntTemple,self.toeend_JntTemple ]
        legSkeleton_all = []
        R_legSkeleton_all = []
        
        
        R_LegjntTemple = []
        
        
        
        
        
        for legS_num  in range(len(legSkeleton)):
            

                R_temple = str(legSkeleton[legS_num]).replace('L_','R_')
                R_LegjntTemple.append( R_temple )
        ###
        L_legReference = 'L_leg_up_reference'
        mc.group(em = True ,n = L_legReference )
        PointC = 'arm_delete_PointC'
        aimC = 'arm_delete_aimC'
        mc.pointConstraint(legSkeleton[0],legSkeleton[2], L_legReference ,n= PointC )
        mc.aimConstraint( legSkeleton[2]  ,L_legReference  ,worldUpType='object',aimVector = (1,0,0), upVector = (0,0,1),worldUpObject = legSkeleton[1] , n =aimC)
        
        mc.delete( PointC , aimC )
        ####
        R_legReference = 'R_leg_up_reference'
        mc.group(em = True ,n = R_legReference )
        PointC = 'leg_delete_PointC'
        aimC = 'leg_delete_aimC'

        mc.pointConstraint(R_LegjntTemple[0],R_LegjntTemple[2], R_legReference ,n= PointC )
        mc.aimConstraint( R_LegjntTemple[2]  ,R_legReference  ,worldUpType='object',aimVector = (-1,0,0), upVector = (0,0,-1),worldUpObject = R_LegjntTemple[1] , n =aimC)
        
        mc.delete( PointC , aimC )
        
        for legS_num  in range(len(legSkeleton)):
            
            if legS_num == len(legSkeleton) - 1:
                skeleton = str(legSkeleton[legS_num]).replace('_Temple','_skeleton')
                legSkeleton_all.append( skeleton )
            else:
                skeleton = str(legSkeleton[legS_num]).replace('_Temple','_skeleton')
                
                
                #mc.aimConstraint( legSkeleton[legS_num + 1] , skeleton ,aimVector = (1,0,0), upVector = (0,1,0),worldUpType = 'vector',worldUpVector= (1,0,0) , n = 'armTemple_deleteConstraint')
                mc.aimConstraint( legSkeleton[legS_num + 1]  , skeleton ,aimVector = (1,0,0), upVector = (0,1,0),worldUpType = 'objectrotation',worldUpObject= L_legReference ,worldUpVector= (0,1,0) , n = 'legTemple_deleteConstraint')
                mc.delete('legTemple_deleteConstraint')

                self.frozeen_jnt_Transform( skeleton )
                legSkeleton_all.append( skeleton )
                
            
            '''
            #``````````````````````````````` mirror leg ````````````````````````````````````````````
            '''
            

            R_skeleton = str( skeleton ).replace('L_','R_')
            R_legSkeleton_all.append( R_skeleton )
            

            if legS_num == len(legSkeleton) - 1:
                pass

            else:
                R_skeleton_Dn = str(legSkeleton[legS_num + 1]).replace('L_','R_')
                #mc.aimConstraint( R_skeleton_Dn , R_skeleton ,aimVector = (-1,0,0), upVector = (0,1,0),worldUpType = 'vector',worldUpVector= (1,0,0) , n = 'armTemple_deleteConstraint')
                mc.aimConstraint( R_skeleton_Dn, R_skeleton ,aimVector = (-1,0,0), upVector = (0,1,0),worldUpType = 'objectrotation',worldUpObject= R_legReference ,worldUpVector= (0,1,0) , n = 'R_legTemple_deleteConstraint')
                
                mc.delete('R_legTemple_deleteConstraint')
                
                self.frozeen_jnt_Transform( R_skeleton )

        mc.delete( L_legReference , R_legReference)
        self.sequenceParent(legSkeleton_all[::-1])
        self.sequenceParent(R_legSkeleton_all[::-1])
        
        ####       
        ankdleSkeleton = str( self.ankle_JntTemple ).replace('_Temple','_skeleton')
        footSkeleton = str( self.foot_JntTemple ).replace('_Temple','_skeleton')
        mc.parentConstraint( footSkeleton,ankdleSkeleton ,n ='deleteCon' ,mo=False,w=1)
        

        mc.delete('deleteCon')
        self.frozeen_jnt_Transform(ankdleSkeleton)
        mc.setAttr(str(ankdleSkeleton)+'.jointOrientY',0)
        
        #
        R_ankle = str(ankdleSkeleton).replace('L_','R_')
        R_foot = str(footSkeleton).replace('L_','R_')
        
        
        mc.orientConstraint( R_foot,R_ankle ,n ='deleteCon',mo=False )
        mc.delete('deleteCon')
        self.frozeen_jnt_Transform(R_ankle)
        mc.setAttr(str(R_ankle)+'.jointOrientY',0)
        
        
        #try:
        #    mc.parent( legSkeleton_all[0] , skeletonGrp)
        #    mc.parent( R_legSkeleton_all[0] , skeletonGrp)
        #except:
        #    pass
        #
        ''' 
        #------------------------------------------------------------
        |||             clavicle 
        '''
        clavicleSkeleton_temple = [ self.clavicle_JntTemple,self.arm_JntTemple ]
        clavicleSkeleton = str(clavicleSkeleton_temple[0]).replace('_Temple','_skeleton')
        
        
        mc.aimConstraint( clavicleSkeleton_temple[1], clavicleSkeleton ,aimVector = (1,0,0), upVector = (0,1,0),worldUpType = 'vector',worldUpVector= (0,1,0) , n = 'armTemple_deleteConstraint')
        mc.delete('armTemple_deleteConstraint')
        self.frozeen_jnt_Transform( clavicleSkeleton )
        
        '''
        #``````````````````````````````` mirror clavicle ````````````````````````````````````````````
        '''
        R_claviclSkeleton_temple = str( clavicleSkeleton_temple[1] ).replace('L_','R_')
        R_clavicleSkeleton = str( clavicleSkeleton ).replace('L_','R_')

        mc.aimConstraint( R_claviclSkeleton_temple, R_clavicleSkeleton ,aimVector = (-1,0,0), upVector = (0,1,0),worldUpType = 'vector',worldUpVector= (0,-1,0) , n = 'armTemple_deleteConstraint')
        mc.delete('armTemple_deleteConstraint')
        self.frozeen_jnt_Transform( R_clavicleSkeleton )
        
        
        ''' 
        #------------------------------------------------------------
        |||             shoulder
        '''
        shoulderSkeleton_temple = [ self.shoulder_JntTemple,self.arm_JntTemple ]
        shoulderSkeleton = str(shoulderSkeleton_temple[0]).replace('_Temple','_skeleton')
        
        mc.aimConstraint( shoulderSkeleton_temple[1], shoulderSkeleton ,aimVector = (1,0,0), upVector = (0,1,0),worldUpType = 'vector',worldUpVector= (0,1,0) , n = 'armTemple_deleteConstraint')
        mc.delete('armTemple_deleteConstraint')
        self.frozeen_jnt_Transform( shoulderSkeleton )
        
        '''
        #``````````````````````````````` mirror shoulder  ````````````````````````````````````````````
        '''
        R_shoulderSkeleton_temple = str( shoulderSkeleton_temple[1] ).replace('L_','R_')
        R_shoulderSkeleton = str( shoulderSkeleton ).replace('L_','R_')
        
        mc.aimConstraint( R_shoulderSkeleton_temple, R_shoulderSkeleton ,aimVector = (-1,0,0), upVector = (0,1,0),worldUpType = 'vector',worldUpVector= (0,-1,0) , n = 'armTemple_deleteConstraint')
        mc.delete('armTemple_deleteConstraint')
        self.frozeen_jnt_Transform( R_shoulderSkeleton )
        